home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / createMotionField.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  5.4 KB  |  175 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //
  22. //  Creation Date:  2003 
  23. //  Author:         Duncan Brinsmead
  24. //
  25. //<doc>
  26. //<name createMotionField>
  27. //<owner "Alias|Wavefront Unsupported">
  28. //
  29. //<synopsis>
  30. //        createMotionField
  31. //
  32. //<returns>
  33. //        None.    
  34. //
  35. //<description>
  36. //    This command creates a force that pushes a fluid based on the motion
  37. //  of an object. One selects the objects that one wishes to push the fluid
  38. //    with and the fluids one wishes to affect. At least one fluid and one 
  39. //  non-fluid must be selected. One will not see an effect until the object
  40. //  is animated. MakeFluidPusher creates a force for each object and sizes the
  41. //  volume boundary of the force to be a bit larger than the object. An
  42. //  expression is generated that sets the direction and magnitude of the 
  43. //  force based on the current motion of the object. The attribute 
  44. //  "pushIntensity" is added to the force node under extra attributes.
  45. //  This allows one to adjust the magnitude of the push force.
  46. //  Note that if one also does a fluid->makeCollide on the object that the
  47. //  pushIntensity may need to be increased, as the force evaluates to zero
  48. //  in the colliding regions of the fluid. This is one reason why 
  49. //  the force bounds to be larger than the object size.
  50. //  Adjusting the scale and volume shape of the force can help to achieve
  51. //  the best effect.
  52. //
  53. //<flags>
  54. //    None.
  55. //
  56. //<examples>
  57. //    // Create a fluid with emitter
  58. //  Create2DContainerEmitter;
  59. //    // Create an animated sphere 
  60. //    sphere;
  61. //  move -r 3 3 0;
  62. //    setKeyframe;
  63. //  currentTime 100;
  64. //  move -r -6 -6 0;
  65. //    setKeyframe;
  66. //  select -r fluidShape1 nurbsSphere1;
  67. //  createMotionField;
  68. //</doc>
  69.  
  70.  
  71. global proc createMotionField()
  72. {
  73.     float  $pushIntensity = 10.0;
  74.     float  $flOverScale = 1.6/2.0;
  75.     float  $maxAspect = 0.1;
  76.  
  77.     string $fluids[] = `ls -sl -dag -type fluidShape`;
  78.     string $selObjs[] = `ls -sl -type transform`;
  79.     string $objs[];    
  80.     int       $numObjs=0;
  81.     int $i;
  82.     for( $i = 0; $i < size($selObjs); $i++ ){
  83.         string $fls[] = `ls -dag -type fluidShape $selObjs[$i]`;
  84.         if( 0 == size( $fls )){
  85.             $objs[$numObjs] = $selObjs[$i];
  86.             $numObjs++;
  87.         }
  88.     }
  89.     if( $numObjs == 0 || size($fluids) == 0 ){
  90.         warning ("createMotionField needs at least one fluid and one object to be selected");
  91.         return;
  92.     }
  93.     string $fields[];
  94.     int $numFields =0;
  95.     for( $i = 0; $i < $numObjs; $i++ ){
  96.         select -r $fluids;
  97.         Uniform;
  98.         $selObjs = `ls -sl`;
  99.         string $field = $selObjs[0];
  100.         $field = `rename $field "motionField#"`;
  101.         $fields[ $numFields ] = $field;
  102.         $numFields++;
  103.         setAttr ($field + ".attenuation") .1;
  104.         setAttr ($field + ".volumeShape") 2;
  105.  
  106.         string $obj = $objs[$i];
  107.         float $bbox[] = `exactWorldBoundingBox $obj`;
  108.         $min[0] = $bbox[0];
  109.         $min[1] = $bbox[1];
  110.         $min[2] = $bbox[2];
  111.         $max[0] = $bbox[3];
  112.         $max[1] = $bbox[4];
  113.         $max[2] = $bbox[5];
  114.  
  115.         float $fsx =$flOverScale *( $max[0] - $min[0] );
  116.         float $fsy =$flOverScale *( $max[1] - $min[1] );
  117.         float $fsz =$flOverScale *( $max[2] - $min[2] );
  118.  
  119.  
  120.         float $maxScale = $fsx;
  121.         if( $maxScale < $fsy ){
  122.             $maxScale = $fsy;
  123.         }
  124.         if( $maxScale < $fsz ){
  125.             $maxScale = $fsz;
  126.         }
  127.         $maxScale *= $maxAspect;
  128.         // avoid flat forces
  129.         if( $fsx < $maxScale ) {
  130.             $fsx = $maxScale;
  131.         }
  132.         if( $fsy < $maxScale ){
  133.             $fsy = $maxScale;
  134.         }
  135.         if( $fsz < $maxScale ){
  136.             $fsz = $maxScale;
  137.         }
  138.  
  139.         setAttr ($field + ".tx") ($min[0] + 0.5 * ($max[0]-$min[0]));
  140.         setAttr ($field + ".ty") ($min[1] + 0.5 * ($max[1]-$min[1]));
  141.         setAttr ($field + ".tz") ($min[2] + 0.5 * ($max[2]-$min[2]));
  142.         setAttr ($field + ".sx") $fsx;
  143.         setAttr ($field + ".sy") $fsy;
  144.         setAttr ($field + ".sz") $fsz;
  145.         parent $field $obj;
  146.  
  147.         addAttr -ln pushIntensity -sn pi -at double -min -0 -max 100 -dv $pushIntensity $field;
  148.         setAttr -e -keyable true ($field + ".pushIntensity");
  149.         string $exp = (
  150.              "float $x = "+$obj+".translateX;\n"
  151.             +"float $y = "+$obj+".translateY;\n"
  152.             +"float $z = "+$obj+".translateZ;\n"
  153.             +"float $to[3] = `getAttr -time (frame - 1.5) "
  154.                 + $obj + ".translate`;\n"
  155.             +"$x -= $to[0];\n"
  156.             +"$y -= $to[1];\n"
  157.             +"$z -= $to[2];\n"
  158.             +"float $len = sqrt( $x*$x + $y*$y + $z*$z );\n"
  159.             +"if( $len > 0.00001 ){\n"
  160.             +"    $x /= $len;\n"
  161.             +"    $y /= $len;\n"
  162.             +"    $z /= $len;\n"
  163.             +"}\n"
  164.             + $field + ".directionX = $x;\n"
  165.             + $field + ".directionY = $y;\n"
  166.             + $field + ".directionZ = $z;\n"
  167.             + $field + ".magnitude = "+$field+".pushIntensity * $len*20;\n"
  168.             );
  169.         expression -s $exp;
  170.         
  171.     }
  172.     select -r $fields;
  173.     
  174. }
  175.